﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using System.Web.Security;

namespace Zebra.Umbraco.Marcos
{
    public partial class MyAccount1 : LoginRequiredControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            //AccountType
            MembershipUser currentUser = Membership.GetUser();
            string[] roles = Roles.GetRolesForUser();
            AccountType.Text = Resources.Zebra.ResourceManager.GetString(roles[0]);
            Email.Text = currentUser.Email;
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                surveyMemberData memberData = db.surveyMemberDatas.First(c => c.nodeId == int.Parse(currentUser.ProviderUserKey.ToString()));
                FullName.Text = memberData.fullName;
                CompanyName.Text = memberData.companyName;
            }


        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MembershipUser currentUser = Membership.GetUser();
            currentUser.Email = Email.Text;
            Membership.UpdateUser(currentUser);
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                surveyMemberData memberData = db.surveyMemberDatas.First(c => c.nodeId == int.Parse(currentUser.ProviderUserKey.ToString()));
                memberData.fullName = FullName.Text;
                memberData.companyName = CompanyName.Text;
                db.SubmitChanges();
            }
            ErrorMessage.Text = "信息保存成功";

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CurrentPassword.Text))
            {
                ErrorMessage.Text = "旧密码不能为空";
                return;
            }
            if (string.IsNullOrEmpty(NewPassword.Text))
            {
                ErrorMessage.Text = "新密码不能为空";
                return;
            }
            if (string.IsNullOrEmpty(ConfirmPassword.Text))
            {
                ErrorMessage.Text = "确认密码不能为空";
                return;
            }
            if (NewPassword.Text != ConfirmPassword.Text)
            {
                ErrorMessage.Text = "请确认新密码与确认密码一致";
            }
            else
            {
                try
                {
                    MembershipUser currentUser = Membership.GetUser();
                    if (currentUser.ChangePassword(CurrentPassword.Text, NewPassword.Text))
                    {
                        ErrorMessage.Text = "密码修改成功";
                    }
                    else
                    {
                        ErrorMessage.Text = "旧密码不正确，请确认";
                    }
                }
                catch (Exception ex)
                {
                    ErrorMessage.Text = ex.Message;
                }
            }
        }
    }
}